<?php
/**
 * Created by PhpStorm.
 * User: yf
 * Date: 2018/3/3
 * Time: 下午6:47
 */

namespace App\Model;


use App\Model\Model;
use App\Bean\User as Bean;
use EasySwoole\Core\Utility\Random;

class User extends Model
{
    protected $table = 'user';

    function register(Bean $bean)
    {
        return $this->dbConnector()->insert($this->table,$bean->toArray());
    }

    function delete(Bean $bean)
    {
        return $this->dbConnector()->where('id',$bean->getId())->delete($this->table);
    }

    function update(Bean $bean,array $data)
    {
        return $this->dbConnector()->where('id',$bean->getId())->update($this->table,$data);
    }

    function updateByUsername(Bean $bean,array $data)
    {
        return $this->dbConnector()->where('username',$bean->getUsername())->update($this->table,$data);
    }

    function login(Bean $bean):?Bean
    {
        $info = $this->dbConnector()->where('username',$bean->getUsername())
            ->where('password',$bean->getPassword())->getOne($this->table);

        if(!empty($info)){
            return new Bean($info);
        }else{
            return null;
        }
    }
}